package com.unione.unione_base.view

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.CoroutineScope
import java.lang.reflect.ParameterizedType

abstract class BaseDataBindingFragment <DB : ViewDataBinding> : Fragment() {
    lateinit  var db: DB

     val mContext: Context
        get() = requireContext()
     val mLifecycleOwner: LifecycleOwner
        get() = this
     val scope: CoroutineScope
        get() = lifecycleScope


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        val type = javaClass.genericSuperclass as ParameterizedType
        val clazz0 = type.actualTypeArguments[0] as Class<DB>
        val method = clazz0.getMethod("inflate", LayoutInflater::class.java)
        db= method.invoke(null, inflater) as DB
        return db.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        init()
    }

    abstract fun init()


}
